package com.cheer.ba.view.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.cheer.ba.R;
import com.cheer.ba.constant.IntentCode;
import com.cheer.ba.constant.IntentKey;
import com.cheer.ba.model.CityModel;
import com.cheer.ba.model.DistrictsModel;
import com.cheer.ba.presenter.impl.OilMealPresenter;
import com.cheer.ba.view.base.BaseCommAty;
import com.cheer.ba.view.base.CommonAdapter;
import com.cheer.ba.view.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 地区第三级
 *
 * @author HQ
 *         created at 2017/5/15 0015 13:25
 */
public class DistrictsAreaAty extends BaseCommAty implements AdapterView.OnItemClickListener {

    @BindView(R.id.id_prov_list)
    ListView idDistrictsListView;

    private CommonAdapter<DistrictsModel> mAdapter;
    private List<DistrictsModel> mDistricts = new ArrayList<DistrictsModel>();

    private CityModel cityModel;

    //记录省级名称
    private String mSaveProvince;

    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.aty_my_area, null, false));
    }

    @Override
    public void initPresenter() {
        presenter = new OilMealPresenter();
    }

    @Override
    public void initView() {
        setTitleText(getString(R.string.mine_area));
        cityModel = (CityModel) getIntent().getExtras().getSerializable(IntentKey.General.KEY_MODEL);
        mSaveProvince = getIntent().getStringExtra(IntentKey.General.KEY_DATA);

        View headView = LayoutInflater.from(this).inflate(R.layout.layout_area_head1, null);
        ((TextView) headView.findViewById(R.id.id_choose_area_txt)).setText("当前城市：" + mSaveProvince + " " + cityModel.mCityName);
        idDistrictsListView.addHeaderView(headView);

        mAdapter = new CommonAdapter<DistrictsModel>(this, mDistricts, R.layout.layout_area_item) {
            @Override
            public void convert(ViewHolder helper, DistrictsModel item) {
                ((TextView) helper.getView(R.id.id_area_check)).setText(item.isDChecked ? "已选中" : "");
                ((TextView) helper.getView(R.id.id_area_text)).setText(item.mDistrictsName);
            }
        };
        idDistrictsListView.setAdapter(mAdapter);
        idDistrictsListView.setOnItemClickListener(this);
    }

    @Override
    public void initData() {
        List<DistrictsModel> districts = (List<DistrictsModel>) ((OilMealPresenter) presenter).getData(6,false);
        if (null != districts && !districts.isEmpty()) {
            getAreaList(districts);
            mAdapter.notifyDataSetChanged();
        }
    }

    /**
     * 更新被选中数据
     */
    public void getAreaList(List<DistrictsModel> districts) {
        mDistricts.clear();
        for (DistrictsModel district : districts) {
            if (TextUtils.equals(cityModel.mCId, district.mCId)) {
                mDistricts.add(district);
            }
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (position != 0) {
            DistrictsModel model = mDistricts.get(position - 1);
            Intent intent = new Intent();
            intent.putExtra(IntentKey.General.KEY_DATA, mSaveProvince + " " + cityModel.mCityName + " " + model.mDistrictsName);
            setResult(IntentCode.Area.DISTRICT_RESULT_CODE, intent);
            finish();
        }
    }
}
